<template>
  <div class="unit-type-manage">
    <el-card shadow="hover">
      <div class="unit-header">
        <el-input
          v-model="searchText"
          placeholder="搜索单位名称/编码"
          clearable
          style="width:200px; margin-right:16px;"
          @keyup.enter="doSearch"
        />
        <el-button type="primary" @click="handleAdd">新增单位</el-button>
      </div>
      <el-table :data="filteredList" border style="margin-top:16px;">
        <el-table-column prop="code" label="单位编码" width="120px"/>
        <el-table-column prop="name" label="单位名称" width="140px"/>
        <el-table-column prop="type" label="单位类型" width="140px"/>
        <el-table-column prop="remark" label="备注" width="200px"/>
        <el-table-column label="操作" width="180px" align="center">
          <template #default="scope">
            <el-button size="small" type="primary" text @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" text @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="400px" destroy-on-close>
      <el-form :model="editRow" :rules="formRules" ref="formRef" label-width="80px">
        <el-form-item label="单位编码" prop="code">
          <el-input v-model="editRow.code" />
        </el-form-item>
        <el-form-item label="单位名称" prop="name">
          <el-input v-model="editRow.name" />
        </el-form-item>
        <el-form-item label="单位类型" prop="type">
          <el-select v-model="editRow.type" placeholder="请选择单位类型">
            <el-option v-for="item in typeOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="editRow.remark" type="textarea"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="submitEdit">保存</el-button>
      </template>
    </el-dialog>

    <!-- 删除弹窗 -->
    <el-dialog v-model="deleteVisible" title="提示" width="320px" center>
      <div>确定要删除该单位吗？</div>
      <template #footer>
        <el-button @click="deleteVisible=false">取消</el-button>
        <el-button type="danger" @click="confirmDelete">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { ElMessage } from 'element-plus'

// 类型定义
interface UnitItem {
  code: string
  name: string
  type: string
  remark: string
}

// 模拟单位类型
const typeOptions = ['重量', '体积', '长度', '面积', '数量', '时间']

// 模拟数据
const unitList = ref<UnitItem[]>([
  { code: 'kg', name: '千克', type: '重量', remark: '国际标准单位' },
  { code: 't', name: '吨', type: '重量', remark: '' },
  { code: 'm3', name: '立方米', type: '体积', remark: '混凝土等体积' },
  { code: 'm', name: '米', type: '长度', remark: '' },
  { code: 'mm', name: '毫米', type: '长度', remark: '' },
  { code: '㎡', name: '平方米', type: '面积', remark: '地面面积' },
  { code: '个', name: '个', type: '数量', remark: '单件计量' },
  { code: '套', name: '套', type: '数量', remark: '设备成套' },
  { code: 'h', name: '小时', type: '时间', remark: '工时' }
])

// 搜索
const searchText = ref('')
const filteredList = computed(() => {
  let arr = unitList.value
  if (searchText.value) {
    arr = arr.filter(
      r =>
        r.name.includes(searchText.value) ||
        r.code.includes(searchText.value)
    )
  }
  return arr
})
function doSearch() {}

// 新增/编辑弹窗
const dialogVisible = ref(false)
const dialogTitle = ref('新增单位')
const editRow = reactive<UnitItem>({
  code: '',
  name: '',
  type: '',
  remark: ''
})
const formRef = ref()
const formRules = {
  code: [{ required: true, message: '必填', trigger: 'blur' }],
  name: [{ required: true, message: '必填', trigger: 'blur' }],
  type: [{ required: true, message: '必选', trigger: 'change' }]
}

function handleAdd() {
  dialogTitle.value = '新增单位'
  Object.assign(editRow, { code: '', name: '', type: '', remark: '' })
  dialogVisible.value = true
}
function handleEdit(row: UnitItem) {
  dialogTitle.value = '编辑单位'
  Object.assign(editRow, row)
  dialogVisible.value = true
}
function submitEdit() {
  formRef.value?.validate((valid: boolean) => {
    if (!valid) return
    if (dialogTitle.value.startsWith('编辑')) {
      const idx = unitList.value.findIndex(t => t.code === editRow.code)
      if (idx !== -1) unitList.value[idx] = { ...editRow }
      ElMessage.success('编辑成功')
    } else {
      unitList.value.unshift({ ...editRow })
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
  })
}

// 删除
const deleteVisible = ref(false)
const deleteTarget = ref<UnitItem | null>(null)
function handleDelete(row: UnitItem) {
  deleteTarget.value = row
  deleteVisible.value = true
}
function confirmDelete() {
  if (deleteTarget.value) {
    unitList.value = unitList.value.filter(t => t.code !== deleteTarget.value!.code)
    ElMessage.success('删除成功')
  }
  deleteVisible.value = false
}
</script>

<style scoped>
.unit-type-manage {
  width: 100%;
  margin: 0;
  padding: 32px 0;
}
.unit-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
</style>
